package com.webex.ta.hydra.gui.macro;

import com.webex.ta.hydra.core.ParamType;

import javax.swing.*;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableColumn;

/**
 * Created by Cisco WebEx.
 * User: vegaz
 * Date: 2010-9-13
 * Time: 16:23:39
 */
public class MacroFieldTable extends JTable {
    public MacroFieldTable() {
        super(new MacroFieldModel());
        this.setRowHeight(20);
        this.getTableHeader().setReorderingAllowed(false);
        this.setShowGrid(true);
    }

    @Override
    public MacroFieldModel getModel() {
        return (MacroFieldModel)super.getModel();
    }

    @Override
    protected void createDefaultEditors() {
        super.createDefaultEditors();
        defaultEditorsByColumnClass.put(ParamType.class, new ParamTypeEditor() );
    }

    public TableCellEditor getCellEditor(int row, int column) {
        TableColumn tableColumn = getColumnModel().getColumn(column);
        TableCellEditor editor = tableColumn.getCellEditor();
        if (editor == null) {
            editor = getDefaultEditor(getColumnClass(column));
        }
        return editor;
    }

    private class ParamTypeEditor extends DefaultCellEditor {
        public ParamTypeEditor() {
            super(new JComboBox(new Object[]{ParamType.LIST, ParamType.TEXT, ParamType.TEXTAREA}));
        }
    }

    public void stopEdit() {
        if (this.isEditing()) {
            this.getCellEditor().stopCellEditing();
        }
    }
}
